package com.ttjj.entity.navi;

import cn.hutool.core.map.MapUtil;
import com.ttjj.entity.map.Position;
import lombok.Getter;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class TerrainNaviMap {
    /**
     * 计算中的路径地图
     */
    @Getter
    private static Map<Integer, TerrainNaviRoute> routeMap = new ConcurrentHashMap<>();
    /**
     * 生效的路径地图
     */
    private static Map<Integer, TerrainNaviRoute> effectiveRouteMap = new ConcurrentHashMap<>();

    public static void reset() {
//        routeMap.clear();
        routeMap = new ConcurrentHashMap<>();

        for (int i = 0; i < 16; ++i) {
            for (int j = 0; j < 28; ++j) {
                Position posA = new Position(i, j);

                for (int m = 0; m < 16; ++m) { // 纵
                    for (int n = 0; n < 28; ++n) { // 横
                        Position posB = new Position(m, n);
                        int hash = TerrainNaviRoute.getRouteHash(posA, posB);
                        if (!routeMap.containsKey(hash)) {
                            routeMap.put(hash, new TerrainNaviRoute(posA, posB));
                        }
                    }
                }
            }
        }

    }

    public static void terrainChange(List<Position> posSet) {
        for (Position posA : posSet) {
            for (int m = 0; m < 16; ++m) {
                for (int n = 0; n < 28; ++n) {
                    Position posB = new Position(m, n);
                    TerrainNaviRoute route = getRoute(posA, posB);
                    route.reset();
                }
            }
        }
    }

    public static TerrainNaviRoute getRoute(Position posA, Position posB) {
        TerrainNaviRoute orDefault = routeMap.getOrDefault(TerrainNaviRoute.getRouteHash(posA, posB), null);
        return orDefault;
    }

    /**
     * 替换最新可用路径图
     */
    public static void setOkRoute() {
        effectiveRouteMap = routeMap;
    }

    /**
     * 获取最新可用路径图
     */
    public static Map<Integer, TerrainNaviRoute> getEffectiveRouteMap() {
        return effectiveRouteMap;
    }

    /**
     * 生效路径图获取路径
     *
     * @param posA
     * @param posB
     * @return
     */
    public static TerrainNaviRoute getEffectiveRoute(Position posA, Position posB) {
        if (MapUtil.isEmpty(effectiveRouteMap)) {
            return null;
        }
        TerrainNaviRoute orDefault = effectiveRouteMap.getOrDefault(TerrainNaviRoute.getRouteHash(posA, posB), null);
        if (orDefault == null) {
            System.out.println(posA + " " + posB);
        }
        return orDefault;
    }

    public boolean equals(Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof TerrainNaviMap)) {
            return false;
        } else {
            TerrainNaviMap other = (TerrainNaviMap) o;
            return other.canEqual(this);
        }
    }

    protected boolean canEqual(Object other) {
        return other instanceof TerrainNaviMap;
    }

    public int hashCode() {
        return 1;
    }

    public String toString() {
        return "TerrainNaviMap()";
    }
}
